package service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import entity.LayRequest;
import entity.SaleOpportunity;
import mapper.SaleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author lay
 * @date 2020/6/18 9:22
 */
@Service
public class SaleServicesImp implements SaleService {
    @Autowired
    private SaleMapper saleMapper;


    @Override
    public int insert(SaleOpportunity saleOpportunity) {
        return saleMapper.insert(saleOpportunity);
    }

    @Override
    public void batchDelete(Integer[] ids) {
        saleMapper.batchDelete(ids);
    }

    @Override
    public int delete(Integer id) {
        return saleMapper.delete(id);
    }

    @Override
    public SaleOpportunity selectById(Integer id) {
        return saleMapper.selectById(id);
    }

    @Override
    public int update(SaleOpportunity saleOpportunity) {
        return saleMapper.update(saleOpportunity);
    }

    @Override
    public PageInfo<SaleOpportunity> selectByPage(Map<String, Object> map) {
        //1.设置当前页，和每页记录条数
        int pageNum = map.get("pageNum")!=null?Integer.parseInt(map.get("pageNum").toString()):1;
        int pageSize = map.get("pageSize")!=null?Integer.parseInt(map.get("pageSize").toString()):2;
        PageHelper.startPage(pageNum,pageSize);
        //2.执行查询语句，startPag之后的第一个查询会默认进行分页
        List<SaleOpportunity> saleOpportunities = saleMapper.selectByMap(map);
        //3.创建PageInfo对象，并将返回结果传入，会自赋值，总记录数当前页等数据
        return new PageInfo<>(saleOpportunities);
    }

    @Override
    public PageInfo<SaleOpportunity> selectByPage(LayRequest layRequest) {
        //1.设置当前页，和每页记录条数
        PageHelper.startPage(layRequest.getPage(),layRequest.getLimit());
        //2.执行查询语句，startPag之后的第一个查询会默认进行分页
        /*List<SaleOpportunity> saleOpportunities = saleMapper.selectAll();*/
        List<SaleOpportunity> saleOpportunities = saleMapper.selectByMap(layRequest.getParams());
        //3.创建PageInfo对象，并将返回结果传入，会自赋值，总记录数当前页等数据
        return new PageInfo<>(saleOpportunities);
    }

    @Override
    public List<SaleOpportunity> selectAll() {
        return saleMapper.selectAll();
    }
}
